package com.app.micaihu.view.user.userinfo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.app.micaihu.R;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.adapter.PagerAdapter.CommonFragmentAdapter;
import com.app.micaihu.view.user.userinfo.fragment.AttentionAndFansPageFragment;
import com.app.utils.stickheaderview.tab.SlidingTabLayout;
import com.app.utils.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Guo on 2016/12/6.
 * <p>
 * 粉丝和关注的 页面
 */

public class AttentionAndFansSwitchPageActivity extends BaseOldActivity {

    private SlidingTabLayout navTab;
    private ViewPager viewPager;
    private List<Fragment> fragments = new ArrayList<>();

    private String type;//0是关注,1是粉丝
    private String toUid;//当前用户的 粉丝/关注
    private CommonFragmentAdapter mAdapter;
    private ArrayList<String> titleList;
    private String[] myTitle = new String[]{"关注", "粉丝"};
    private String[] otherTitle = new String[]{"TA的关注", "TA的粉丝"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getIntentData();
        setTitleShow(false);
        addContentView(R.layout.activity_attention_and_fans_switchpage);
        initView();

    }


    private void getIntentData() {
        if (getIntent() != null) {
            Intent intent = getIntent();
            type = intent.getStringExtra(Config.Extra.PARAMETER1);
            toUid = intent.getStringExtra(Config.Extra.PARAMETER2);
        }
    }

    private void initView() {
        navTab = (SlidingTabLayout) findViewById(R.id.nav_tab);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (fragments != null) fragments.clear();
        if (titleList == null) {
            titleList = new ArrayList<>();

        }
        titleList.clear();
        for (int i = 1; i <= 2; i++) {
            AttentionAndFansPageFragment attentionAndFansPageFragment = AttentionAndFansPageFragment.newInstance(i + "", toUid);
            fragments.add(attentionAndFansPageFragment);
        }
        if (mAdapter == null) {
            if (UserInforManager.getInstance().isLogin()
                    && TextUtils.equals(UserInforManager.getInstance().getUserInfor().getUid(), toUid)) {
                mAdapter = new CommonFragmentAdapter(getSupportFragmentManager(), fragments, myTitle);
            } else {
                mAdapter = new CommonFragmentAdapter(getSupportFragmentManager(), fragments, otherTitle);
            }

            viewPager.setAdapter(mAdapter);
            viewPager.setOffscreenPageLimit(fragments.size());
            navTab.setViewPager(viewPager);

            viewPager.setCurrentItem(StringUtils.toInt(type, 0));
            if (StringUtils.toInt(type, 0) == 0) {
                navTab.setTabSelected(0);
            }

        }

    }


}
